<?php

use app\assets\EchartsAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\AgentPackage;
use app\models\KeyMap;
use yii\db\Expression;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $card app\models\Card
 */

EchartsAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '卡流量使用周期统计';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<table class="table table-striped table-bordered table-hover">
    <tr>
        <th>ICCID</th>
        <td><?php echo $card->iccid;?></td>
    </tr>
    <tr>
        <th>代理商</th>
        <td><?php if (empty($card->agent_id)) {?>
                <i>未分配</i>
            <?php } else {
                echo Html::a(Html::encode($card->agent->username), ['/admin/agent/agent-view', 'id' => $card->agent_id]);
            }?>
            </td>
    </tr>
    <tr>
        <th>状态</th>
        <td><?php echo KeyMap::getValue('card_status', $card->status);?></td>
    </tr>

</table>
<div style="text-align: center;margin-bottom:5px;">
<button class="btn btn-danger btn-sm" onclick="reGenerate(<?php echo $card->id;?>)" title="重新生成周期流量">重新生成</button>
</div>

<table class="table table-striped table-bordered table-hover">
        <tr>
            <th colspan="3"><h4 style="color: #CA5952;">用量信息(<?php echo Yii::$app->formatter->asDate($card->active_time);?> ---- <?php echo Yii::$app->formatter->asDate($card->term_end_time);?> )</h4></th>
        </tr>
        <tr>
            <th>数据(已用/全部)</th>   
            <td><?php $dataPercent = $card->total_flow == 0 ? 100 : $card->data * 100 / $card->total_flow;?>
            <?php echo Yii::$app->formatter->asShortSize($card->data), ' / ', Yii::$app->formatter->asShortSize($card->total_flow);?>
                <div class="progress" style="margin-bottom: 3px; min-width: 200px;">
                    <div class="progress-bar progress-bar-<?php echo $dataPercent < 80 ? 'success' : ($dataPercent < 100 ? 'warning' : 'danger');?> progress-bar-striped" role="progressbar" aria-valuenow="<?php echo $card->data;?>" aria-valuemin="0" aria-valuemax="<?php echo $card->total_flow;?>" style="width: <?php echo $dataPercent;?>%">
                        
                    </div>
                </div>
            </td>
        </tr>
        <tr>
        <th>套餐</th>
        <td><?php if (empty($card->package_id)) {?>
                <i>未设置</i>
            <?php } else {
                echo Html::a(Html::encode($card->package->name), ['/admin/card/package-edit', 'id' => $card->package_id]);
                // echo '（成本价格：', $card->package->price , '）';
                if (!empty($card->agent_id)) {
                    $agentPackage = AgentPackage::findOne(['agent_id' => $card->agent_id, 'package_id' => $card->package_id]);
                    // if (!empty($agentPackage)) {
                    //     echo '（代理商成本价：', $agentPackage->cost_price, '）';
                    //     echo '（代理商销售价：', $agentPackage->sale_price, '）';
                    // } else {
                    //     echo '（代理商价格未设置）';
                    // }
                }
                
            }?></td>
    </tr>
    </table>
<?php foreach ($subList as $sub) {?>
    <table class="table table-striped table-bordered table-hover">
        <tr>
            <th colspan="3"><h4 style="color: #CA5952;">用量信息(<?php echo Yii::$app->formatter->asDate($sub->start_date);?> ---- <?php echo Yii::$app->formatter->asDate($sub->end_date);?> )</h4></th>
        </tr>
        <tr>
            <th>数据(已用/全部)</th>   
            <td><?php $dataPercent = $sub->package->data == 0 ? 100 : $sub->data * 100 / $sub->package->data;?>
            <?php echo Yii::$app->formatter->asShortSize($sub->data), ' / ', Yii::$app->formatter->asShortSize($sub->package->data);?>
                <div class="progress" style="margin-bottom: 3px; min-width: 200px;">
                    <div class="progress-bar progress-bar-<?php echo $dataPercent < 80 ? 'success' : ($dataPercent < 100 ? 'warning' : 'danger');?> progress-bar-striped" role="progressbar" aria-valuenow="<?php echo $sub->data;?>" aria-valuemin="0" aria-valuemax="<?php echo $sub->package->data;?>" style="width: <?php echo $dataPercent;?>%">
                        
                    </div>
                </div>
            </td>
        </tr>
        <tr>
        <th>套餐</th>
        <td><?php if (empty($sub->package_id)) {?>
                <i>未设置</i>
            <?php } else {
                echo Html::a(Html::encode($sub->package->name), ['/admin/card/package-edit', 'id' => $sub->package_id]);
                // echo '（成本价格：', $card->package->price , '）';
                if (!empty($card->agent_id)) {
                    $agentPackage = AgentPackage::findOne(['agent_id' => $card->agent_id, 'package_id' => $sub->package_id]);
                    // if (!empty($agentPackage)) {
                    //     echo '（代理商成本价：', $agentPackage->cost_price, '）';
                    //     echo '（代理商销售价：', $agentPackage->sale_price, '）';
                    // } else {
                    //     echo '（代理商价格未设置）';
                    // }
                }
                
            }?></td>
    </tr>
    </table>
   
    
<?php }?>
<script>
    /**
     * 重新生成卡周期流量使用信息
     * @param id 卡编号
     */      
    function reGenerate(id) {
           $.getJSON('<?php echo Url::to(['/admin/finance/re-generate-usage']);?>', {id:id}, function (json) {
               if(json.code == "1"){
                 layer.msg(json.msg, function () {window.location.reload();});
               }else{
                   layer.msg(json.msg);
               }
            });
    }
</script>
